package cn.tedu._07springaop.egaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CartAspect {
    @Before(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("1.开始添加购物车之前");
    }
    @After(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public void after(JoinPoint joinPoint){
        System.out.println("2.操作完成");
    }

    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))",returning = "result")
    public void returning(JoinPoint joinPoint,boolean result){
        if (result){
            System.out.println("3.操作成功");
        }else {
            System.out.println("3.操作失败");
        }
    }
    //@Around(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        boolean result =false;
        try {
            System.out.println("1.开始添加购物车之前");
            result = (boolean)joinPoint.proceed();
            System.out.println("3.操作成功");
        } catch (Throwable e) {
            System.out.println("操作失败");
            throw new RuntimeException(e);
        }finally {
            System.out.println("2.操作完成");
        }
        return result;
    }
}
